﻿@using SelfMadeMillionaire.Core
@using SelfMadeMillionaire.Core.Enums
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.Formatters
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Newss
@using PagedList.Mvc
@model NewsListViewModel

<div class="table-responsive">
	@if (Model.News.Any())
 {
		<table class="table table-bordered tablesorter">
			<thead>
				<tr>
					<th><a href="#">Date</a></th>
					<th><a href="#">Product</a></th>
                    <th><a href="#">Level</a></th>
					<th><a href="#">Title</a></th>
					<th><a href="#">External link</a></th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody>
				@foreach (var item in Model.News)
	{
					<tr>
						<td>@item.Date.ToString("MM/dd/yyyy")</td>
						<td>@ProductTypeFormatter.GetProductTypeName((ProductType)Enum.Parse(typeof(ProductType), item.ProductType.ToString()))</td>
						<td>@Html.Raw(DateSentFormatter.FormatMinAccessLevel(item))</td>
                        <td title="@item.Tooltip">@item.Title</td>
						<td><a href="@item.ExternalUrl" target="_blank" title="@item.Tooltip">@item.ExternalUrl</a></td>
						<td>
                            <a href="@Url.Action("Edit", "News", new { id = item.ID })" class="edit">Edit</a>
							| <a href="javascript:void(0);"  onclick="if (confirm('Are you sure you want to delete this news?')) { Delete(@(item.ID)) }">Delete</a>
						</td>
					</tr>
	}
			</tbody>
		</table>
        
		@Html.PagedListPager(Model.News, page => Url.Action("Index", new { page, startDate = Model.StartDate, endDate = Model.EndDate, selectedProduct = Model.ProductTypes.SelectedValue, title = Model.Title, minAccessLevel = Model.AccessLevels.SelectedValue, sort = Model.SortOrder }))
 }
 else
 {
		<p>No records found.</p>
 }
</div>

<script type="text/javascript">
	$(function () {
	    $("#news-list table").tablesorter({
	        headers: {
	            5: { sorter: false }
	        }
	    });

		$("a.edit").click(function () {
			var startDate = $("#@Html.IdFor(m=>m.StartDate)").val();
			var endDate = $("#@Html.IdFor(m=>m.EndDate)").val();
			var selectedProduct = $("#@Html.IdFor(m => m.SelectedProduct)").val();
		    var title = $("#@Html.IdFor(m => m.Title)").val();
		    var minAccessLevel = $("#@Html.IdFor(m => m.MinAccessLevel)").val();
		    var sort = $("#@Html.IdFor(m => m.SortOrder)").val();
		    
		    $(this).attr("href", $(this).attr("href") + "?startDate=" + startDate + "&endDate=" + endDate + "&selectedProduct=" + selectedProduct + "&title=" + title + "&minAccessLevel=" + minAccessLevel + "&sort=" + sort);
		});

	    var sortRefresh = true;
	    @if(!string.IsNullOrEmpty(Model.SortOrder))
        {
            <text>
	            sortRefresh = false;
	            setInitSort("news-list", '@(Model.SortOrder)');
            </text>
        }

	    $("#news-list table").on("sortEnd", function (event) {
	        $("#SortOrder").val(event.target.config.sortList);
	        if (sortRefresh)
	            GetList();
	        else
	            sortRefresh = true;

	    });
	});
</script>
